import { getCommodityDetail } from '../../../../services/commodity';
import * as log from '../../../../utils/log';

Component({
  mixins: [],
  data: {
    selectedShippingMethodId: '',
    selectedOptionIds: [],
    currentCommodity: {},
    purchaseNumber: 1,
    address: '杭州市 余杭区  闲林街道  翡翠城东北区',
  },
  props: {
    show: false,
    onClose: () => {},
    onConfirm: () => {},
    commodityId: '',
  },
  didMount() {},
  didUpdate(preProps) {
    const { commodityId } = this.props;
    if (preProps.commodityId !== commodityId) {
      this.fetchCommodityDetailById(commodityId);
    }
  },
  didUnmount() {},
  methods: {
    onClose() {
      this.props.onClose();
    },
    async fetchCommodityDetailById(id) {
      const initRsp = await getCommodityDetail(id);
      let data = initRsp.data;
      const goodsDetail = await this.goodsDetail(id);
      data.id = goodsDetail.result._id;
      data.cover = goodsDetail.result.picUrl;
      data.commodityName = goodsDetail.result.name;
      data.price = goodsDetail.result.price;
      data.shopeId = goodsDetail.result.shopeId;
      data.shopeName = goodsDetail.result.shopeName;
      console.log('goodsDetail',data)
      this.setData({
        currentCommodity: data,
        selectedShippingMethodId: this.getFirstId(data.shippingMethod),
        selectedOptionIds: data.options.map(item =>
          this.getFirstId(item.values)
        ),
      })
      // getCommodityDetail(id)
      //   .then(({ data }) =>
      //     this.setData({
      //       currentCommodity: data,
      //       selectedShippingMethodId: this.getFirstId(data.shippingMethod),
      //       selectedOptionIds: data.options.map(item =>
      //         this.getFirstId(item.values)
      //       ),
      //     })
      //   )
      //   .catch(err =>
      //     log.error(
      //       'CommodityDrawer.fetchCommodityDetailById.getCommodityDetail',
      //       err
      //     )
      //   );
    },
    //这个地方获取商品数据
    async goodsDetail(id) {
      const goodsDetail    =  await my.fncontext.callFunction({
        name:"goodsDetail",
        data:{
          productId:id
        }
      });
      console.log('goodsDetail one',goodsDetail)
      return goodsDetail;
    },
    getFirstId(list = []) {
      if (list.length > 0) {
        return list[0].id;
      }
      return null;
    },
    onShippingMethodSelect(id) {
      this.setData({ selectedShippingMethodId: id });
    },
    onOptionSelect(id, index) {
      const nextSelectedOptionIds = [...this.data.selectedOptionIds];
      nextSelectedOptionIds[index] = id;
      this.setData({ selectedOptionIds: nextSelectedOptionIds });
    },
    onPurchaseNumberChange(num) {
      this.setData({ purchaseNumber: num });
    },
    async onConfirm() {
      const app = getApp();
      const userInfo = app.globalData.userInfo;
      console.log(userInfo);
      if(userInfo==null){
        abridge.alert({
          title: "获取个人信息失败，请稍后"
        })
        return
      }
      const currentCommodity  = this.data.currentCommodity;
      console.log("currentCommodity",currentCommodity)
      const addCar= {
        "name": currentCommodity.commodityName,
        "price": currentCommodity.price,
        "number":  this.data.purchaseNumber,
        "picUrl": currentCommodity.cover,
        "productId": currentCommodity.id,
        "shopeId": currentCommodity.shopeId,
        "shopeName": currentCommodity.shopeName,
        "description": currentCommodity.type,
        "userId": userInfo.user_id
      }
      const shopCarAdd    =  await my.fncontext.callFunction({
        name:"shopppingCarAdd",
        data:addCar
      });
      console.log("shopCarAdd",shopCarAdd)
      this.props.onConfirm();
    },
  },
});
